// 正确的导入方式
import axios, { type AxiosResponse } from 'axios';

export interface LSTMTrainParams {
    target?: string;
    test_size?: number;
    epochs?: number;
    batch_size?: number;
    look_back?: number;
    forecast_horizon?: number;
}

export interface LSTMTrainResponse {
    status: 'success' | 'error';
    metrics?: {
        mse?: number;
        mae?: number;
        rmse?: number;
        r2?: number;
    };
    plots?: {
        training?: string;
    };
    model?: string;
    params?: {
        epochs: number;
        batch_size: number;
        test_size: number;
        look_back: number;
        forecast_days: number;
    };
    message?: string;
    details?: string;
}

/**
 * 训练LSTM模型
 * @param params 训练参数
 * @returns 训练结果
 */
export const trainLSTMModel = async (
    params: LSTMTrainParams
): Promise<LSTMTrainResponse> => {
    try {
        const response: AxiosResponse<LSTMTrainResponse> = await axios.get(
            'http://127.0.0.1:5000/api/train_lstm',
            {
                params: {
                    target: params.target || 'guilin_temp',
                    test_size: params.test_size || 0.2,
                    epochs: params.epochs || 100,
                    batch_size: params.batch_size || 64,
                    look_back: params.look_back || 180,
                    forecast_horizon: params.forecast_horizon || 90,
                },
            }
        );

        return response.data;
    } catch (error) {
        if (axios.isAxiosError(error) && error.response) {
            return error.response.data;
        }
        return {
            status: 'error',
            message: '请求失败，请检查网络连接',
        };
    }
};